package Comp355Final.main;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class GUIInternetChapterSelection extends Activity{
	private Config myconfig;
	private String target_book;
	private String target_url;
	private String book_id;
	private ArrayList<String> bookchapter_name = new ArrayList<String>();
	private ArrayList<String> bookchapter_url = new ArrayList<String>();
	private ArrayList<String> bookchapter_provider = new ArrayList<String>();
	private ArrayList<String> bookchapter_site = new ArrayList<String>();
	private int listenOnID;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    	myconfig = new Config(this);
    	Bundle extras = getIntent().getExtras();
    	target_book = extras.getString("target_book");
    	target_url = extras.getString("target_url");
    	book_id = extras.getString("book_id");
    	listenOnID = extras.getInt("listenOnID",0);
    	//Utility.quick_dialog(target_url, this);
    	ScrollView scllview = new ScrollView(this);
    	ScrollView.LayoutParams scllview_param = new ScrollView.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.FILL_PARENT);
        scllview.setBackgroundColor(Color.WHITE);
        scllview.setBackgroundDrawable(myconfig.return_bg());
    	LinearLayout Llay = new LinearLayout(this);
        LinearLayout.LayoutParams Llay_param = new LinearLayout.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.FILL_PARENT);
    	Llay.setOrientation(LinearLayout.VERTICAL);
    	TCPGatherChapterSelection tgcs = new TCPGatherChapterSelection(target_url,this);
    	boolean connected_or_not = tgcs.connect_toTarget();
    	if(connected_or_not)
    	{
    	//Utility.quick_dialog("Book Selected", this);
    		bookchapter_name=tgcs.get_bookchapter_name();
    		bookchapter_url =tgcs.get_bookchapter_url();
    		this.bookchapter_provider=tgcs.get_bookchapter_provider();
    		this.bookchapter_site=tgcs.get_bookchapter_site();
    		for(int i=0;i<bookchapter_name.size();i++)
    		{
    			LinearLayout tmp_l_lay = new LinearLayout(this);
    	        LinearLayout.LayoutParams tmp_l_lay_param = new LinearLayout.LayoutParams(
    	        		ViewGroup.LayoutParams.FILL_PARENT,
    	        		ViewGroup.LayoutParams.WRAP_CONTENT);
    	        TextView tmp_txtview = new TextView(this);
    	        tmp_txtview.setText(bookchapter_name.get(i));
    	        tmp_txtview.setTextColor(Color.BLACK);
    	        PanelOnTouchListener potl = new PanelOnTouchListener(tmp_txtview);
    	        tmp_l_lay.setClickable(true);
    	        tmp_l_lay.setOnTouchListener(potl);
    	        bookchapter_url.set(i, "http://www.booksky.org/ViewBook.aspx?SiteID="+this.bookchapter_site.get(i)+"&BookID="+book_id+"&ChapterID="+this.bookchapter_url.get(i));
    	        InternetChClickListener iccl = new InternetChClickListener(this,bookchapter_url.get(i),target_book,bookchapter_name.get(i));
    	        tmp_l_lay.setOnClickListener(iccl);
    	        tmp_l_lay.addView(tmp_txtview);
    	        Llay.addView(tmp_l_lay, tmp_l_lay_param);
    			
    		}
    	}
    	
    	this.setTitle(target_book + ": Chapter Selection");
        scllview.addView(Llay, Llay_param);
        this.setContentView(scllview, scllview_param);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) { 
    	menu.add(0, 0, 0, "Find a book").setIcon(android.R.drawable.ic_menu_search);
    	menu.add(0, 1, 1, "Return to BookShelf").setIcon(android.R.drawable.ic_menu_revert); 
      
       menu.add(0, 2, 2, "Setting").setIcon(android.R.drawable.ic_menu_preferences);
       return super.onCreateOptionsMenu(menu); 
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	if(item.getItemId()==0)
    	{
    		showDialog(DIALOG_TEXT_ENTRY);
    	}
    	else if(item.getItemId()==1)
    	{
    		Intent int_ent = new Intent(this, GUIBookShelfBkSelect.class);
    		this.startActivity(int_ent);
    		finish();
    	}
    	else if(item.getItemId()==2)
    	{
    		Intent int_ent = new Intent(this, GUIConfig.class);
    		this.startActivity(int_ent);
    	}
    	return super.onOptionsItemSelected(item); 
    }
    private static final int DIALOG_TEXT_ENTRY = 7;
    @Override   
    protected Dialog onCreateDialog(int id) {
    	switch(id){
    case DIALOG_TEXT_ENTRY:
    	// This example shows how to add a custom layout to an AlertDialog
    	LayoutInflater factory = LayoutInflater.from(this);
    	final EditText search_box = new EditText(this);
    	final View textEntryView = search_box;
    	return new AlertDialog.Builder(GUIInternetChapterSelection.this)
    	//.setIconAttribute(android.R.attr.alertDialogIcon)
    	.setTitle("Search Book")
    	.setView(textEntryView)
    	.setPositiveButton("ok", new DialogInterface.OnClickListener() {
    		public void onClick(DialogInterface dialog, int whichButton) {
    			/* User clicked OK so do some stuff */
    				String tmp_encrypted_str = search_box.getText().toString();
    				//Utility.quick_dialog(search_box.getText().toString(), GUIInternetBookSelection.this);
    				search_box.setText("");
    				if(tmp_encrypted_str.equals(""))
    				{
    					Utility.quick_dialog("You must search something.", GUIInternetChapterSelection.this);
    					return;
    				}
    				try {
    					
						String encode_phase=URLEncoder.encode(tmp_encrypted_str,"utf-8");
						Intent int_ent = new Intent(GUIInternetChapterSelection.this, GUIInternetBookSelection.class);
						int_ent.putExtra("encode_phase",encode_phase );
						int_ent.putExtra("analysis_mode", 2);
						int_ent.putExtra("listenOnID", listenOnID);
//listenOnID
						GUIInternetChapterSelection.this.startActivity(int_ent);
//analysis_mode
						
						//Utility.quick_dialog(encode_phase, GUIInternetBookSelection.this);
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						//e.printStackTrace();
					}
    			} 
    		})
    		.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
    			public void onClick(DialogInterface dialog, int whichButton) {
    				search_box.setText("");
    				/* User clicked cancel so do some stuff */
    				} 
    			})
    			.create();
    	}
    	return null;
    	}
    	
}
